package com.ziq.baselib.test.page

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.binding
import com.ziq.base.view.OverLayoutManager
import com.ziq.base.view.recycleview.BaseMultiViewHolder
import com.ziq.baselib.R
import com.ziq.baselib.databinding.ActivityLayoutManagerBinding
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.*
import java.util.concurrent.TimeUnit

class CustomLayoutManagerActivity : BaseActivity() {

    private val binding : ActivityLayoutManagerBinding by binding()

    var disposable: Disposable? = null
    private var list : ArrayList<DataItem> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        for (i in 0..3){
            list.add(DataItem(i.toString()))
        }
        binding.recycleView.layoutManager = OverLayoutManager()
        binding.recycleView.adapter = Adapter(list)

        Observable.interval(2000,2000, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Long> {
                override fun onComplete() {
                }

                override fun onSubscribe(d: Disposable) {
                    disposable = d
                }

                override fun onNext(t: Long) {
                    Log.e("ziq", String.format("onNext %s", t))
                    list.add(0,DataItem(list.size.toString()))
                    binding.recycleView.adapter!!.notifyItemInserted(0)
                }

                override fun onError(e: Throwable) {
                }

            })
    }

    override fun onDestroy() {
        disposable?.dispose()
        super.onDestroy()
    }

    //数据类
    private data class DataItem(var name: String)


    private class Adapter(var data:ArrayList<DataItem>) : RecyclerView.Adapter<BaseMultiViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, p1: Int): BaseMultiViewHolder {
            Log.e("ziq", "onCreateViewHolder")
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout_manager, parent, false)
            return BaseMultiViewHolder(view)
        }

        override fun getItemCount(): Int {
            return data.size
        }

        override fun onBindViewHolder(holder: BaseMultiViewHolder, position: Int) {
            Log.e("ziq", "===onBindViewHolder")
            holder.getViewById<TextView>(R.id.tv_name).text = "======${data.size}"
        }
    }

    companion object{
        fun getRandColor():String{
            var R:String
            var G:String
            var B:String

            val random = Random()
            R = Integer.toHexString(random.nextInt(256)).toUpperCase();
            G = Integer.toHexString(random.nextInt(256)).toUpperCase();
            B = Integer.toHexString(random.nextInt(256)).toUpperCase();
            R =  if(R.length == 1) "0$R" else R;
            G =  if(G.length == 1) "0$G" else G;
            B =  if(B.length == 1) "0$B" else R;
            return "#$R$G$B"
        }
    }
}